package com.jd.ty.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.jws.WebMethod;

import com.jd.ty.dao.CustomerCompanyDAO;
import com.jd.ty.dao.CustomerDAO;
import com.jd.ty.dao.DictionaryDAO;
import com.jd.ty.dao.EmployeeDAO;
import com.jd.ty.dao.OrderDAO;
import com.jd.ty.dao.ProjectAttrbuteDAO;
import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.constant.QueryConstants.Order;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.Customer;
import com.jd.ty.pojo.CustomerCompany;
import com.jd.ty.pojo.CustomerOrder;
import com.jd.ty.pojo.Dictionary;
import com.jd.ty.pojo.Employee;
import com.jd.ty.pojo.ProjectAttrValue;
import com.jd.ty.util.Pager;
import com.jd.ty.web.filter.CheckOrderFilter;

public class CustomerOrderService {

	private OrderDAO orderDAO;
	private CustomerCompanyDAO customerCompanyDAO;
	private CustomerDAO customerDAO;
	private DictionaryDAO dictionaryDAO;
	private EmployeeDAO employeeDAO;

	public List<CustomerOrder> getList(Pager pager, String filter, String sort) {
		List<CustomerOrder> orders = orderDAO.getByQuery(pager, filter, sort);
		for (CustomerOrder customerOrder : orders) {
			orderDAO.initialize(customerOrder.getCustomer());
			orderDAO.initialize(customerOrder.getCustomer().getCustCompany());
			orderDAO.initialize(customerOrder.getEmployee());
		}
		return orders;
	}

	public List<CustomerOrder> getList(String filter) {
		List<CustomerOrder> orders = orderDAO.getByQuery(filter);
		for (CustomerOrder customerOrder : orders) {
			orderDAO.initialize(customerOrder.getCustomer());
			orderDAO.initialize(customerOrder.getCustomer().getCustCompany());
			orderDAO.initialize(customerOrder.getEmployee());
		}
		return orders;
	}

	public void checkList(List<Map<String, Object>> set) {
		
		HqlCriterions hql = new HqlCriterions();
		for (Map<String, Object> map : set) {
			
			String companyName = (String) map
					.get(CheckOrderFilter.MAP_KEY_COMPANY_NAME);
			if (!"".equals(companyName)) {
				hql.or( QueryConstants.CustomerCompany.CUST_COMPANY_NAME,
						companyName, QueryConstants.LIKE);
			}
		}
		if(hql.getHqlString()==null){
			set.clear();
			return;
		}
			/* REMOVE AT 2012-04-11, FOR REQUIREMENT CHANGE
			String customerName = (String) map
					.get(CheckOrderFilter.MAP_KEY_CUSTOMER_NAME);
			if (!"".equals(customerName)) {
				hql.and(Order.CUSTOMER + '.'
						+ QueryConstants.Customer.CUSTOMER_NAME, customerName,
						QueryConstants.EQ);
			}
			String position = (String) map
					.get(CheckOrderFilter.MAP_KEY_CUSTOMER_POSITION);
			if (!"".equals(position)) {
				hql
						.and(Order.CUSTOMER + '.'
								+ QueryConstants.Customer.POSITION, position,
								QueryConstants.EQ);
			}
			String type = (String) map
					.get(CheckOrderFilter.MAP_KEY_PROJECT_TYPE);
			if (null != type) {
				hql.and(Order.PROJECT_TYPE + '.'
						+ QueryConstants.Dictionary.DICTIONARY_NAME, type,
						QueryConstants.EQ);
			}
			*/
			
			
			//hql.and(Order.DEAL_DATE, cal.getTime(), QueryConstants.GE);
		List<CustomerCompany> companys = (List<CustomerCompany>) customerCompanyDAO
				.getByQuery(hql.getHqlString());

		set.clear();
		for (CustomerCompany company : companys) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put(CheckOrderFilter.MAP_KEY_COMPANY_NAME, company
					.getCustCompanyName());

			hql.clear();
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			cal.add(Calendar.DAY_OF_YEAR, -365);
			hql.and(Order.DEAL_DATE, cal.getTime(), QueryConstants.GE);
			hql.and(Order.CUSTOMER + '.' + QueryConstants.Customer.CUST_COMPANY
					+ '.' + QueryConstants.CustomerCompany.CUST_COMPANY_NAME,
					company.getCustCompanyName(), QueryConstants.EQ);
			long count = orderDAO.getCountByQuery(hql.getHqlString());
			if (count > 0) {
				map.put(CheckOrderFilter.MAP_KEY_STATUS, "不可用");
			} else {
				map.put(CheckOrderFilter.MAP_KEY_STATUS, "可用");
			}

			set.add(map);
		}
			
		return;
	}
	
	public void updateOrder(CustomerOrder norder) {
		CustomerOrder order = (CustomerOrder) orderDAO.loadById(norder
				.getOrderId());
		if (null != norder.getCustomer().getCustomerId()) {
			Customer customer = (Customer) customerDAO.loadById(norder
					.getCustomer().getCustomerId());
			order.setCustomer(customer);
			if (null != norder.getCustomer().getCustCompany()
					.getCustCompanyId()) {
				CustomerCompany custCompany = (CustomerCompany) customerCompanyDAO
						.loadById(norder.getCustomer().getCustCompany()
								.getCustCompanyId());
				customer.setCustCompany(custCompany);
			}
		}

		if (null != order.getEmployee().getEmployeeId()) {
			Employee employee = (Employee) employeeDAO.loadById(order
					.getEmployee().getEmployeeId());
			order.setEmployee(employee);
		} else {
			order.setEmployee(null);
		}

		order.setDealDate(norder.getDealDate());
		Dictionary projectType = (Dictionary) dictionaryDAO.getById(norder
				.getProjectType().getDictionaryId());
		order.setProjectType(projectType);
		order.setScheduledDate(norder.getScheduledDate());
		Dictionary status = (Dictionary) dictionaryDAO.getById(norder
				.getStatus().getDictionaryId());
		order.setStatus(status);
		order.setOrderSerial(norder.getOrderSerial());
		List<ProjectAttrValue> values = order.getProjectAttrValues();
		for (ProjectAttrValue projectAttrValue : values) {
			projectAttrValue.setCustomerOrder(null);
			orderDAO.delete(projectAttrValue);
		}
		for (ProjectAttrValue projectAttrValue : norder.getProjectAttrValues()) {
			projectAttrValue.setCustomerOrder(order);
		}
		order.setProjectAttrValues( norder.getProjectAttrValues());
	}

	public void addOrder(CustomerOrder order) {

		// company exist or not

		HqlCriterions hql = new HqlCriterions();
		hql.and(QueryConstants.CustomerCompany.CUST_COMPANY_NAME, order
				.getCustomer().getCustCompany().getCustCompanyName(),
				QueryConstants.EQ);
		CustomerCompany company = (CustomerCompany) customerCompanyDAO
				.getOneByQuery(hql.getHqlString(), false);

		if (company != null) {
			List<Customer> customers = company.getCustomers();
			for (Customer customer : customers) {
				if (customer.getCustomerName().equals(
						order.getCustomer().getCustomerName())) {
					order.setCustomer((Customer) customerDAO.loadById(customer
							.getCustomerId()));
					break;
				}
			}
			CustomerCompany custCompany = (CustomerCompany) customerCompanyDAO
					.loadById(company.getCustCompanyId());
			order.getCustomer().setCustCompany(custCompany);

		}

		if (null != order.getEmployee()
				&& order.getEmployee().getEmployeeId() != null) {
			Employee employee = (Employee) employeeDAO.loadById(order
					.getEmployee().getEmployeeId());
			order.setEmployee(employee);
		} else {
			order.setEmployee(null);
		}

		Dictionary projectType = (Dictionary) dictionaryDAO.getById(order
				.getProjectType().getDictionaryId());
		order.setProjectType(projectType);

		Dictionary status = (Dictionary) dictionaryDAO.getById(order
				.getStatus().getDictionaryId());
		order.setStatus(status);
		List<ProjectAttrValue> values = order.getProjectAttrValues();
		for (ProjectAttrValue projectAttrValue : values) {
			projectAttrValue.setCustomerOrder(order);
		}
		if ("".equals(order.getOrderSerial())) {
			order.setOrderSerial(UUID.randomUUID().toString());
		}
		orderDAO.saveOrUpdate(order);
	}

	public void deleteOrder(Long[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				CustomerOrder order = (CustomerOrder) orderDAO.loadById(ids[i]);
				orderDAO.delete(order);
			}
		}

	}

	public CustomerOrder get(long id) {
		return (CustomerOrder) orderDAO.getById(id);
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

	public void setCustomerCompanyDAO(CustomerCompanyDAO customerCompanyDAO) {
		this.customerCompanyDAO = customerCompanyDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public void setCustomerCompanyService(
			CustomerCompanyService customerCompanyService) {
	}

	public void setDictionaryDAO(DictionaryDAO dictionaryDAO) {
		this.dictionaryDAO = dictionaryDAO;
	}

	public void setProjectAttrbuteDAO(ProjectAttrbuteDAO projectAttrbuteDAO) {
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public List<CustomerCompany> getCompanyList(String companyName) {
		HqlCriterions hql = new HqlCriterions();
		hql.and(QueryConstants.CustomerCompany.CUST_COMPANY_NAME, companyName,
				QueryConstants.LIKE);
		List<CustomerCompany> companies = customerCompanyDAO.getByQuery(hql
				.getHqlString());
		return companies;
	}

	public List<Customer> getCustomerList(String companyName) {
		HqlCriterions hql = new HqlCriterions();
		hql.and(QueryConstants.Customer.CUST_COMPANY + '.'
				+ QueryConstants.CustomerCompany.CUST_COMPANY_NAME,
				companyName, QueryConstants.LIKE);
		List<Customer> customers = customerDAO.getByQuery(hql.getHqlString());
		return customers;
	}

}
